/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.FacultyDAL;
import mnstudent.datatranferobject.FacultyDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXTable;
/**
 *
 * @author lee
 */
public class FacultyBLL {
   FacultyDAL facultyBLL;
    
    public FacultyBLL()
    {
        facultyBLL = new FacultyDAL();
    }
     /**
     * Ham lay tat ca cac khoa
     *
     * @return boolean
     */
    public ResultSet getAllFaculty() {
        
        return facultyBLL.getAllFaculty();
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getFacultyById(String _id)
    {
        return facultyBLL.getFacultyById(_id);
    }
    
    public ArrayList<FacultyDTO> getAllFacultyToArray()
    {
        ArrayList<FacultyDTO> arrFaculty = new ArrayList<FacultyDTO>();
        try {
            ResultSet rs = getAllFaculty();
            while(rs.next())
            {
                FacultyDTO faculty = new FacultyDTO(rs.getNString(1), rs.getNString(2));
                arrFaculty.add(faculty);
            }
        } catch (SQLException ex) {
            Logger.getLogger(FacultyBLL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return arrFaculty;
    }
    /**
     * ham them moi khoa xuong database
     * @param _faculty
     * @return true: success, false: fail
     */
    public boolean insertFacultyToDatabase(FacultyDTO _faculty)
    {
        return facultyBLL.insertFacultyToDatabase(_faculty);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _faculty
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateFacultyToDatabase(FacultyDTO _faculty)
    {
          return facultyBLL.updateFacultyToDatabase(_faculty);
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _faculty
     * @return 
     */
     public boolean deleteFacultyFromDatabase(FacultyDTO _faculty)
    {
        return facultyBLL.deleteFacultyFromDatabase(_faculty);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
}
